package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description NursingServiceImpl
 * @Author liangDong
 * @Date 2025-05-07  20:40
 */

@Service
public class NursingProjectServiceImpl implements NursingService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> page(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectByPage(name,status);
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        List<NursingProjectVo> nursingProjectVoList = projectVoPageResponse.getRecords();
        List<Map<String, Object>> countList = nursingProjectMapper.getCountList();
        Map<Long, Integer> countMap = countList.stream().collect(Collectors.toMap(
                map -> ((Number) map.get("id")).longValue(),
                map -> ((Number) map.get("number")).intValue()
        ));
        nursingProjectVoList.forEach(vo ->vo.setCount(countMap.getOrDefault(vo.getId(),0)));
        return projectVoPageResponse;
    }

    @Override
    public void add(NursingProjectVo nursingProjectVo) {
        nursingProjectVo.setCreateBy(1L);
        nursingProjectVo.setCreateTime(LocalDateTime.now());
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.select(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectVo nursingProjectVo) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProject.setUpdateBy(1L);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setStatus(status);
        nursingProject.setId(id);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(1L);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProject> getAll() {
        List<NursingProject> nursingProjectList= nursingProjectMapper.selectAll();
        //List<NursingProjectVo> list = (List<NursingProjectVo>) nursingProjectList;
        return nursingProjectList;
    }
}
